package com.atguigu.jxc.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.TreeNode;
import com.atguigu.jxc.entity.Unit;
import com.atguigu.jxc.service.GoodsService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @PostMapping("goods/listInventory")
    public Map<String, Object> listInventory(HttpServletRequest request) {
        Integer page = Integer.valueOf(request.getParameter("page"));
        Integer rows = Integer.valueOf(request.getParameter("rows"));
        String codeOrName = request.getParameter("codeOrName");
        String goodsTypeId = request.getParameter("goodsTypeId");
        Map<String, Object> map = goodsService.getListInventory(page, rows, codeOrName, goodsTypeId);
        return map;
    }
    ///goodsType/loadGoodsType

    /**
     * 请求URL：http://localhost:8080/goodsType/loadGoodsType
     * 请求参数：无
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：String
     */
    @PostMapping("goodsType/loadGoodsType")
    public TreeNode[] loadGoodsType() {
        TreeNode treeNode = goodsService.loadGoodsType();
        return new TreeNode[]{treeNode};
    }

    /**
     * 请求URL：http://localhost:8080/unit/list
     * 请求参数：无
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：Map<String,Object>
     */
    @PostMapping("unit/list")
    public Map<String, Object> unitList() {
        List<Unit> list = goodsService.getUnitList();
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows", list);
        return map;
    }

    /**
     * 请求URL：http://localhost:8080/goods/list
     * 请求参数：Integer page, Integer rows, String goodsName, Integer goodsTypeId
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：Map<String,Object>
     */
    @PostMapping("goods/list")
    public Map<String, Object> list(HttpServletRequest request) {
//        Integer page, Integer rows, String goodsName, Integer goodsTypeId
        Integer page = Integer.valueOf(request.getParameter("page"));
        Integer rows = Integer.valueOf(request.getParameter("rows"));
        String goodsName = request.getParameter("goodsName");
        String goodsTypeId = request.getParameter("goodsTypeId");
        Map<String, Object> map = goodsService.getListInventory(page, rows, goodsName, goodsTypeId);
        return map;
    }

    /**
     * 请求URL：http://localhost:8080/goodsType/save
     * 请求参数：String  goodsTypeName,Integer  pId
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：ServiceVO
     */
    @PostMapping("goodsType/save")
    public ServiceVO saveType(String goodsTypeName, Integer pId) {
        try {
            goodsService.saveType(goodsTypeName, pId);
            return new ServiceVO(100, "请求成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return new ServiceVO(200, "请求失败!");
        }
    }

    /**
     * 请求URL：http://localhost:8080/goodsType/delete
     * 请求参数：Integer  goodsTypeId
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：ServiceVO
     */
    @PostMapping("goodsType/delete")
    public ServiceVO delType(Integer goodsTypeId) {
        try {
            goodsService.delType(goodsTypeId);
        } catch (Exception e) {
            e.printStackTrace();
            return new ServiceVO(200, "请求失败!");
        }
        return new ServiceVO(100, "请求成功!");
    }

    /**
     * 请求URL：http://localhost:8080/goods/save?goodsId=37
     * 请求参数：Goods goods
     * 请求方式：POST
     * 返回值类型：JSON
     */
    @PostMapping("goods/save")
    public ServiceVO saveGoods(Goods goods) {
        try {
            goodsService.saveGoods(goods);
            return new ServiceVO(100, "请求成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return new ServiceVO(200, "请求失败!");
        }
    }

    /**
     * 请求URL：http://localhost:8080/goods/delete
     * 请求参数：Integer goodsId
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：ServiceVO
     */
    @PostMapping("goods/delete")
    public ServiceVO delGoods(Integer goodsId) {
        Goods goods = goodsService.getGoods(goodsId);
        if (null != goods) {
            Integer state = goods.getState();
            if (state == 0) {
                goodsService.delGoods(goodsId);
            } else {
                return new ServiceVO(200, "不能删除(请更改状态)!");
            }
        }
        return new ServiceVO(100, "请求成功!");
    }

    /**
     * http://localhost:8080/goods/getCode
     */
    @PostMapping("goods/getCode")
    public Map<String,String> getCode() {
        return goodsService.getCode();

//        ArrayList<String> codes = goodsMapper.getGoodsCode();
//        Integer s = Integer.valueOf(codes.get(codes.size() - 1));
//        ++s;
//        goods.setGoodsCode(s.toString());
    }

    /**
     * 请求URL：http://localhost:8080/goods/getNoInventoryQuantity
     * 请求参数：Integer page,Integer rows,String nameOrCode
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：Map<String,Object>
     */
    @PostMapping("goods/getNoInventoryQuantity")
    public Map<String, Object> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        Map<String, Object> map = goodsService.getNoInventoryQuantity(page, rows, nameOrCode);
        return map;
    }

    /**
     * 请求URL：http://localhost:8080/goods/getHasInventoryQuantity
     * 请求参数：Integer page,Integer rows,String nameOrCode
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：Map<String,Object>
     */
    @PostMapping("goods/getHasInventoryQuantity")
    public Map<String, Object> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        Map<String, Object> map = goodsService.getHasInventoryQuantity(page, rows, nameOrCode);
        return map;
    }

    /**
     * 请求URL：http://localhost:8080/goods/saveStock?goodsId=25
     * 请求参数：Integer goodsId,Integer inventoryQuantity,double purchasingPrice
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：ServiceVO
     */
    @PostMapping("goods/saveStock")
    public ServiceVO saveStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
        try {
            goodsService.saveStock(goodsId, inventoryQuantity, purchasingPrice);
        } catch (Exception e) {
            e.printStackTrace();
            return new ServiceVO(200, "请求失败!");
        }
        return new ServiceVO(100, "请求成功!");
    }

    /**
     * 请求URL：http://localhost:8080/goods/deleteStock
     * 请求参数：Integer goodsId
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：ServiceVO
     */
    @PostMapping("goods/deleteStock")
    public ServiceVO deleteStock(Integer goodsId) {
        Goods goods = goodsService.getGoods(goodsId);
        Integer state = goods.getState();
        if (0 == state) {
            goodsService.deleteStock(goodsId);
            return new ServiceVO(100, "请求成功!");
        } else {
            return new ServiceVO(200, "不能删除(请更改状态)!!");
        }
    }
}